package operations.impl;

import java.io.Serializable;

import corba_manager.CorbaManager;

import data.DataObject;

import operations.Operation;
import operations.OperationType;
import operations.Replicated;

public class OperationFactory {
	/**
	 * construction de l'opération
	 * 
	 * @param target
	 *            : nom du topic cible
	 * @param type
	 *            : create|delete|update
	 * @param rep
	 *            : est ou n'es pas repliqué
	 * @param obj
	 *            : la donnée
	 * @return : l'opération construite
	 */
	public Operation buildOperation(String target, OperationType type, Replicated rep, Serializable obj){
		if(target.equals("topic")){
			if(type.getValue() == OperationType._create){
				Operation operation = new Operation(OperationType._create, "topic", rep.getValue(), 
						new DataObject(CorbaManager.getInstance().createAny()));
				operation.getData().getData().insert_Value(obj);
				return operation;
			}
			if(type.getValue() == OperationType._delete){
				Operation operation = new Operation(OperationType._delete, "topic", rep.getValue(), 
						new DataObject(CorbaManager.getInstance().createAny()));
				operation.getData().getData().insert_Value(obj);
				return operation;
			}
		}
		else if(target.equals("cache")){
			Operation operation = new Operation(type.getValue(), "cache", rep.getValue(), 
					new DataObject(CorbaManager.getInstance().createAny()));
			operation.getData().getData().insert_Value(obj);
			return operation;
		}
		return null;
	}
}
